package com.echo.controller;

import com.echo.dto.SmsRequest;
import com.echo.result.ResultVO;
import com.echo.service.impl.EmailService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import com.echo.enumeration.ResultEnum;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/account")
public class AccountController {

    private final EmailService emailService;

    public AccountController(EmailService emailService) {
        this.emailService = emailService;
    }

    @PostMapping("/sms")
    public ResultVO<Map<String, String>> sendCaptcha(@RequestBody SmsRequest smsRequest) {
        String email=smsRequest.getEmail();
        ResultVO<Map<String, String>> resultVO;
        System.out.println("进入邮箱了");
        // 校验邮箱格式
        if (email == null || !email.matches("^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\\.[a-zA-Z0-9-.]+$")) {
            return new ResultVO<>(0, "邮箱格式不正确", null);
        }

        try {
            // 生成并发送验证码
            String captcha = emailService.generateAndSendCaptcha(email);
            Map<String, String> response = new HashMap<>();
            response.put("captcha", "不可以偷看噢"); // 在测试环境返回验证码

            // 发送成功返回结果
            resultVO = new ResultVO<>(0, "验证码发送成功", response);
        } catch (Exception e) {
            // 捕获异常返回错误信息
            resultVO = new ResultVO<>(1, "验证码发送失败：" + e.getMessage(), null);
        }

        return resultVO;
    }

}
